package controller.version;


import mapper.info.InfoMapper;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import pojo.Devuser;
import pojo.Info;
import pojo.Version;
import service.info.InfoService;
import service.version.VersionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/version")
public class VersionController {
    @Resource
    private VersionService versionService;
    @Resource
    private InfoService infoService;
    @RequestMapping("/getAllAppVersion")
    public String adddevRom(Model model,int id)
    {
        List<Version> appVersionList=versionService.getAllAppVersion(id);
        Info in = infoService.getAppInfohuiXian(id);
        model.addAttribute("in",in);
        model.addAttribute("appVersionList",appVersionList);
        return "devAddRom";
    }
    //新增版本信息
    @RequestMapping("/addversionsave")
    public String addversionsave(Version version,HttpServletRequest request, @RequestParam(value ="idPic",required = false)MultipartFile idPic){
        String idPicPath=null;
        String path=request.getSession().getServletContext().getRealPath("statics"+ File.separator+"uploadfiles");
        if (idPic!=null) {
            if (!idPic.isEmpty()) {
                String originalFile = idPic.getOriginalFilename();
                String prefix = FilenameUtils.getExtension(originalFile);
                int filesize = 5000000;
                if (idPic.getSize() > filesize) {
                    request.setAttribute("uploadFileError", "*daxiao");
                    return "devAddRom";
                } else if (prefix.equalsIgnoreCase("apk")) {
                    String fileName = System.currentTimeMillis() + RandomUtils.nextInt(10000000) + "_Personal." + prefix;
                    File tragetFile = new File(path);
                    if (!tragetFile.exists()) {
                        tragetFile.mkdirs();
                    }
                    try {
                        idPic.transferTo(new File(tragetFile, fileName));
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("uploadFileError", "上传失败！");
                        return "devAddRom";
                    }
                    idPicPath = File.separator + "statics" + File.separator + "uploadfiles" + File.separator + fileName;
                    version.setApkFileName(fileName);
                } else {
                    request.setAttribute("uploadFileError", "格式不正确");
                    return "devAddRom";
                }

            }
        }
            version.setCreationDate(new Date());
            version.setCreatedBy(((Devuser) request.getSession().getAttribute("devuser")).getId());
            version.setPublishStatus(3);
            version.setApkLocPath(idPicPath);
            int num = versionService.addAppVersion(version);
            version.setId(versionService.updateAppInfo(version.getId(),version.getAppId()));
        if (num > 0) {
                return "userKaiaudit";
            } else {
                return "devAddRom";
            }
    }
    //修改的回显功能

    @RequestMapping("/getupdateAllAppVersion")
    public String getupdateAllAppVersion(Model model,int id)
    {
        List<Version> appVersionList=versionService.getAllAppVersion(id);
        Info in = infoService.getAppInfohuiXian(id);
        model.addAttribute("in",in);
        model.addAttribute("appVersionList",appVersionList);
        Version version=versionService.getVersionListByID(in.getVersionId());
        model.addAttribute("version",version);
        return "devUpdateRom";
    }
    //修改版本信息
    @RequestMapping("/updateversionsave")
    public String updateversionsave(Version version,HttpServletRequest request, @RequestParam(value ="idPic",required = false)MultipartFile idPic){
        String idPicPath=null;
        String path=request.getSession().getServletContext().getRealPath("statics"+ File.separator+"uploadfiles");
        if (idPic!=null) {
            if (!idPic.isEmpty()) {
                String originalFile = idPic.getOriginalFilename();
                String prefix = FilenameUtils.getExtension(originalFile);
                int filesize = 5000000;
                if (idPic.getSize() > filesize) {
                    request.setAttribute("uploadFileError", "*daxiao");
                    return "devUpdateRom";
                } else if (prefix.equalsIgnoreCase("apk")) {
                    String fileName = System.currentTimeMillis() + RandomUtils.nextInt(10000000) + "_Personal." + prefix;
                    File tragetFile = new File(path);
                    if (!tragetFile.exists()) {
                        tragetFile.mkdirs();
                    }
                    try {
                        idPic.transferTo(new File(tragetFile, fileName));
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("uploadFileError", "上传失败！");
                        return "devUpdateRom";
                    }
                    idPicPath = File.separator + "statics" + File.separator + "uploadfiles" + File.separator + fileName;
                    version.setApkFileName(fileName);
                } else {
                    request.setAttribute("uploadFileError", "格式不正确");
                    return "devUpdateRom";
                }
            }
        }

        version.setCreationDate(new Date());
        version.setCreatedBy(((Devuser) request.getSession().getAttribute("devuser")).getId());
        version.setPublishStatus(3);
        version.setApkLocPath(idPicPath);
        int num = versionService.updateVersion(version);
        if (num > 0) {
            return "userKaiaudit";
        } else {
            return "devUpdateRom";
        }
    }


}
